package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceResourceDAO;
import com.ourteam.workspace.dao.WorkspaceResource;
import com.ourteam.workspace.dao.WorkspaceResourceDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:33 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceResourceDAOJdbcImpl extends AbstractSpringDAO
    implements IWorkspaceResourceDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        Description,
        OrderIndex,
        ResourcePath,
        ResourceType,
        Status,
        WorkspaceId,
        Name,
        WorkspaceName,
        WorkspacePath
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.Description,
        _DataObjectProperty.OrderIndex,
        _DataObjectProperty.ResourcePath,
        _DataObjectProperty.ResourceType,
        _DataObjectProperty.Status,
        _DataObjectProperty.WorkspaceId,
        _DataObjectProperty.Name,
        _DataObjectProperty.WorkspaceName,
        _DataObjectProperty.WorkspacePath
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<WorkspaceResource, WorkspaceResourceDAOQueryBean> daoHelper;

  public AbstractWorkspaceResourceDAOJdbcImpl() {
    super();
    daoHelper = new DataAccessObjectHelper<WorkspaceResource, WorkspaceResourceDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.workspace.dao.IWorkspaceDAO.EntityName)
          .append(" workspaceA ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.workspace.dao.IWorkspaceDAO.Id,
              com.ourteam.workspace.dao.IWorkspaceDAO.EntityName + ".",
              "workspaceA."));
      relatedJoinSQLBuffer.append(" = ").append(WorkspaceId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspaceResource
   * @throws Exception
   */
  public boolean checkIsUnique(WorkspaceResource aWorkspaceResource) throws Exception {

    if (aWorkspaceResource == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspaceResource.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspaceResource
   * @throws Exception
   */
  public void insert(WorkspaceResource aWorkspaceResource) throws Exception {

    super.doInsert(aWorkspaceResource);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspaceResource
   * @throws Exception
   */
  public void batchInsert(WorkspaceResource[] aWorkspaceResources) throws Exception {

    super.doBatchInsert(aWorkspaceResources);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceResourceDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(WorkspaceResource aWorkspaceResource) throws Exception {

    delete(aWorkspaceResource.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspaceResource
   * @throws Exception
   */
  public void update(WorkspaceResource aWorkspaceResource) throws Exception {

    if (aWorkspaceResource.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspaceResource);

    WorkspaceResource tempObj = findById(aWorkspaceResource.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspaceResource, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaceResources[]
   * @throws Exception
   */
  public void batchUpdate(WorkspaceResource[] aWorkspaceResources) throws Exception {

    super.doBatchUpdate(aWorkspaceResources);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspaceResource
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(WorkspaceResource aWorkspaceResource, WorkspaceResourceDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aWorkspaceResource, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceResourceDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(WorkspaceResource aWorkspaceResource) throws Exception {

    if (aWorkspaceResource.getDescription() == null) {
      throw new Exception("The WorkspaceResource.description value is not allow NULL");
    }

    if (aWorkspaceResource.getResourcePath() == null) {
      throw new Exception("The WorkspaceResource.resourcePath value is not allow NULL");
    }

    if (aWorkspaceResource.getResourceType() == null) {
      throw new Exception("The WorkspaceResource.resourceType value is not allow NULL");
    }

    if (aWorkspaceResource.getStatus() == null) {
      throw new Exception("The WorkspaceResource.status value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public WorkspaceResource findById(final long id) throws Exception {

    WorkspaceResourceDAOQueryBean queryBean = new WorkspaceResourceDAOQueryBean();

    WorkspaceResourceDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    WorkspaceResource[] WorkspaceResources = query(queryBean);

    if (WorkspaceResources.length == 0) {
      return null;
    } else {
      return WorkspaceResources[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return WorkspaceResource[]
   * @throws Exception
   */
  public WorkspaceResource[] query(WorkspaceResourceDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceResourceRowMapper());

    return (WorkspaceResource[]) result.toArray(new WorkspaceResource[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(WorkspaceResourceDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceResourceDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      WorkspaceResource[] aWorkspaceResources, WorkspaceResourceDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aWorkspaceResources, queryBean);
  }

  public void synchronize(
      WorkspaceResource[] aWorkspaceResources,
      WorkspaceResourceDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<WorkspaceResource> callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaceResources, queryBean, callBack);
  }

  private com.ourteam.workspace.dao.impl.AbstractWorkspaceResourceBusinessPackageRelateDAOJdbcImpl
      WorkspaceResourceBusinessPackageRelateDao;

  public void setWorkspaceResourceBusinessPackageRelateDao(
      com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO
          WorkspaceResourceBusinessPackageRelateDao) {
    this.WorkspaceResourceBusinessPackageRelateDao =
        (com.ourteam.workspace.dao.impl.AbstractWorkspaceResourceBusinessPackageRelateDAOJdbcImpl)
            WorkspaceResourceBusinessPackageRelateDao;
  }

  /**
   * Query Related WorkspaceResourceBusinessPackageRelate
   *
   * @param queryBean
   * @throws Exception
   */
  public com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelate[]
      queryWorkspaceResourceBusinessPackageRelate(WorkspaceResourceDAOQueryBean queryBean)
          throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO
                .WorkspaceResourceId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.EntityName
            + " ref_WorkspaceResourceBusinessPackageRelate";

    String[] entityFields =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.ENTITY_FIELDS;

    String[] entityProperties =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.ENTITY_PROPERTIES;

    List list =
        WorkspaceResourceBusinessPackageRelateDao.doQuery(
            entityName,
            entityFields,
            entityProperties,
            queryBean,
            joinSQL.toString(),
            new com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO
                .JdbcWorkspaceResourceBusinessPackageRelateRowMapper());

    return (com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelate[])
        list.toArray(
            new com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelate[list.size()]);
  }

  /**
   * Query Related WorkspaceResourceBusinessPackageRelate Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] queryWorkspaceResourceBusinessPackageRelateSelective(
      WorkspaceResourceDAOQueryBean queryBean) throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.Id, "id");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.BusinessPcakageId,
          "businessPcakageId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.Description,
          "description");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.Status, "status");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.WorkspaceResourceId,
          "workspaceResourceId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.LastGenCodeTime,
          "lastGenCodeTime");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.SubmitterId,
          "submitterId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO.SubmitterName,
          "submitterName");
    }

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO
                .WorkspaceResourceId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.EntityName
            + " ref_WorkspaceResourceBusinessPackageRelate";

    return WorkspaceResourceBusinessPackageRelateDao.doQuerySelective(
        entityName, queryBean, joinSQL.toString());
  }

  /**
   * Update Related WorkspaceResourceBusinessPackageRelate
   *
   * @param aWorkspaceResourceBusinessPackageRelate
   * @param aQueryBean
   * @model
   * @throws Exception
   */
  public void updateWorkspaceResourceBusinessPackageRelate(
      com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelate
          aWorkspaceResourceBusinessPackageRelate,
      WorkspaceResourceDAOQueryBean aQueryBean)
      throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO
                .WorkspaceResourceId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.EntityName
            + " ref_WorkspaceResourceBusinessPackageRelate";

    String[] entityFields =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.ENTITY_FIELDS;

    String[] entityProperties =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO.ENTITY_PROPERTIES;

    WorkspaceResourceBusinessPackageRelateDao.doUpdateByQuery(
        entityName,
        entityFields,
        entityProperties,
        joinSQL.toString(),
        aWorkspaceResourceBusinessPackageRelate,
        aQueryBean);
  }

  /**
   * Delete Related WorkspaceResourceBusinessPackageRelate
   *
   * @param aQueryBean
   * @model
   * @throws Exception
   */
  public void deleteWorkspaceResourceBusinessPackageRelate(WorkspaceResourceDAOQueryBean aQueryBean)
      throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceResourceBusinessPackageRelateDAO
                .WorkspaceResourceId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceResourceBusinessPackageRelateDAO
            .EntityName; // + " ref_WorkspaceResourceBusinessPackageRelate";

    WorkspaceResourceBusinessPackageRelateDao.doDeleteByQuery(
        entityName, joinSQL.toString(), aQueryBean);
  }

  public static class JdbcWorkspaceResourceRowMapper implements RowMapper<WorkspaceResource> {

    public WorkspaceResource mapRow(ResultSet resultSet, int arg1) throws SQLException {
      WorkspaceResource object = new WorkspaceResource();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object description = null;

        description = resultSet.getObject(columnIndex++);

        object.setDescriptionNull(resultSet.getBoolean(columnIndex++));

        if (description != null) {

          setBeanValue(object, _DataObjectProperty.Description, description);
        }

        Object orderIndex = null;

        orderIndex = resultSet.getObject(columnIndex++);

        object.setOrderIndexNull(resultSet.getBoolean(columnIndex++));

        if (orderIndex != null) {

          setBeanValue(object, _DataObjectProperty.OrderIndex, orderIndex);
        }

        Object resourcePath = null;

        resourcePath = resultSet.getObject(columnIndex++);

        object.setResourcePathNull(resultSet.getBoolean(columnIndex++));

        if (resourcePath != null) {

          setBeanValue(object, _DataObjectProperty.ResourcePath, resourcePath);
        }

        Object resourceType = null;

        resourceType = resultSet.getObject(columnIndex++);

        object.setResourceTypeNull(resultSet.getBoolean(columnIndex++));

        if (resourceType != null) {

          setBeanValue(object, _DataObjectProperty.ResourceType, resourceType);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object workspaceId = null;

        workspaceId = resultSet.getObject(columnIndex++);

        object.setWorkspaceIdNull(resultSet.getBoolean(columnIndex++));

        if (workspaceId != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceId, workspaceId);
        }

        Object name = null;

        name = resultSet.getObject(columnIndex++);

        object.setNameNull(resultSet.getBoolean(columnIndex++));

        if (name != null) {

          setBeanValue(object, _DataObjectProperty.Name, name);
        }

        Object workspaceName = null;

        workspaceName = resultSet.getObject(columnIndex++);

        object.setWorkspaceNameNull(resultSet.getBoolean(columnIndex++));

        if (workspaceName != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceName, workspaceName);
        }

        Object workspacePath = null;

        workspacePath = resultSet.getObject(columnIndex++);

        object.setWorkspacePathNull(resultSet.getBoolean(columnIndex++));

        if (workspacePath != null) {

          setBeanValue(object, _DataObjectProperty.WorkspacePath, workspacePath);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
